//
//  GXOrderTestViewController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/5.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXCommonOrderDetailController.h"
#import "GXContentMenuCell.h"
#import "GXContentMenuHeaderView.h"
#import "GXCommonClientMessageCell.h"
#import "GXCommonOrderInformationViewCell.h"
#import "OrderAndUploadPhotoMananger.h"
#import "HttpHelper.h"
#import "GXContentMessageFooterView.h"
#import "GXContentMoreFooterView.h"
@interface GXCommonOrderDetailController ()<UITableViewDataSource,UITableViewDelegate,ContentMessageFooterDelegate,ContentMoreFooterDelegate>
@property(nonatomic,strong) UITableView *detailTableView;
@property(nonatomic,strong) NSArray *dishesData;
@property(nonatomic,assign) BOOL showAll;
@property(nonatomic,strong) NSMutableDictionary *dataSourceDict;
@end
static NSString *const contentMenuCell=@"GXContentMenuCell";
static NSString *const clientMessageCell=@"GXCommonClientMessageCell";
static NSString *const commonOrderInformationViewCell=@"GXCommonOrderInformationViewCell";
@implementation GXCommonOrderDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kGlobalbgColor;
    self.title = @"订单详情";
    [self requestData];
    [self.view addSubview:self.detailTableView];
}
-(void)requestData
{
    [OrderAndUploadPhotoMananger selectShopOrderAllMessageWithONumber:self.onumber success:^(id responseObject) {
        [self handleOrderMessageDataFromResponseObject:responseObject];
     
    } fail:^(id responseObject) {
    }];
}
- (void)handleOrderMessageDataFromResponseObject:(id)responseObject
{
    self.dataSourceDict = [NSMutableDictionary dictionaryWithDictionary:responseObject[@"result"]];
    NSString *dishesStr = responseObject[@"result"][@"dishes"];
    NSArray *array = [dishesStr componentsSeparatedByString:@","];
    self.dishesData = [NSArray arrayWithArray:array];
    if (self.dishesData.count > 3) {
        self.showAll = YES;
    }else{
        self.showAll = NO;
    }
    [self.detailTableView reloadData];
}

- (UITableView *)detailTableView
{
 
    if (_detailTableView == nil) {
        _detailTableView=[[UITableView alloc] initWithFrame:CGRectMake(5,0,kUIScreenWidth-10,kUIScreenHeight-Height_NavBar)  style:UITableViewStyleGrouped];
        _detailTableView.delegate=self;
        _detailTableView.dataSource=self;
        _detailTableView.showsHorizontalScrollIndicator=NO;
        _detailTableView.showsVerticalScrollIndicator=NO;
        _detailTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [_detailTableView registerNib:[UINib nibWithNibName:@"GXContentMenuCell" bundle:nil] forCellReuseIdentifier:contentMenuCell];
        [_detailTableView registerNib:[UINib nibWithNibName:@"GXCommonClientMessageCell" bundle:nil] forCellReuseIdentifier:clientMessageCell];
        [_detailTableView registerNib:[UINib nibWithNibName:@"GXCommonOrderInformationViewCell" bundle:nil] forCellReuseIdentifier:commonOrderInformationViewCell];
    }
    return _detailTableView;
}

#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==1) {
        if (self.showAll) {
            return  3;
        }
        return self.dishesData.count;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==0) {
        GXCommonClientMessageCell * clientCell = [tableView dequeueReusableCellWithIdentifier:clientMessageCell];
        if (self.dataSourceDict) {
          clientCell.clientDict = [NSDictionary dictionaryWithDictionary:self.dataSourceDict];
        }
        return clientCell;
    }else if (indexPath.section ==1)
    {
        GXContentMenuCell* cell = [tableView dequeueReusableCellWithIdentifier:contentMenuCell];
        if (self.dishesData.count) {
            cell.dishes = self.dishesData[indexPath.row];
        }
        return cell;
    }else if (indexPath.section ==2)
    {
        GXCommonOrderInformationViewCell * ordCell = [tableView dequeueReusableCellWithIdentifier:commonOrderInformationViewCell];
        if (self.dataSourceDict) {
            ordCell.orderDict = [NSDictionary dictionaryWithDictionary:self.dataSourceDict];
        }
        return  ordCell;
    }
    return nil;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0) {
        return 143;
    }else if (indexPath.section ==1) {
        return 56;
    }else if (indexPath.section ==2) {
        return 260;
    }
    return 0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:false];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==1) {
        GXContentMenuHeaderView *header = [GXContentMenuHeaderView headView];
        return header;
    }else{
        UIView * headerView = [[UIView alloc]init];
        return headerView;
    }
    return nil;

}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section ==1) {
        if (self.dishesData.count > 3) {
            GXContentMoreFooterView * footer  = [GXContentMoreFooterView  footerView];
            footer.delegate = self;
              footer.footDict = [NSDictionary dictionaryWithDictionary:self.dataSourceDict];
            footer.showAll = self.showAll;
            footer.statuButtonBlock= ^(BOOL showAll)
            {
                if (self.dishesData.count > 3) {
                    self.showAll = !showAll;
                }
                [self.detailTableView reloadData];
            };
            return footer;
        }else
        {
            GXContentMessageFooterView *footer = [GXContentMessageFooterView messageFooterView];
            footer.delegate = self;
            footer.footDict = [NSDictionary dictionaryWithDictionary:self.dataSourceDict];
            return footer;
        }
    }else{
        UIView * footerView = [[UIView alloc]init];
        footerView.backgroundColor = [UIColor clearColor];
        return footerView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section ==1) {
        if (self.dishesData.count > 3) {
              return  84;
        }
        return 47;
    }else{
        return 15;
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section ==1) {
        return  37;
    }else{
        return 15;
    }
}

#pragma ContentMessageFooterDelegate
- (void)pressButtonChangeOrderState:(NSString *)stateTag
{
    self.dataSourceDict[@"state"] =stateTag;
    [self.detailTableView reloadData];
}
#pragma ContentMoreFooterDelegate
- (void)pressButtonRefreshOrderState:(NSString *)stateTag
{
    self.dataSourceDict[@"state"] =stateTag;
    [self.detailTableView reloadData];
}
@end
